
#include "desktop.h" 


// #include "../images/images.h"




// #include "../../api/sys/sys.h"


#include "../apps/apps.h"


void AppIcon::set_text(const char* text)
{
  if (label != NULL)
  {
    label->set_text(text, THEME_COLOR_RED);
  }
}


AppIcon::AppIcon(UI_Object* parent) :Container(parent)
{
  this->set_size(72, 92);

  this->container = new Container(this);


  this->image = new Image(this->container);
  this->label = new Label(this);

  lv_obj_align(label->get_lv_obj_t(), LV_ALIGN_CENTER, 0, -10);

  lv_obj_add_flag(this->image->get_lv_obj_t(), LV_OBJ_FLAG_EVENT_BUBBLE | LV_OBJ_FLAG_EVENT_BUBBLE);
  lv_obj_add_flag(this->container->get_lv_obj_t(), LV_OBJ_FLAG_EVENT_BUBBLE | LV_OBJ_FLAG_EVENT_BUBBLE);
  lv_obj_add_flag(this->label->get_lv_obj_t(), LV_OBJ_FLAG_EVENT_BUBBLE | LV_OBJ_FLAG_EVENT_BUBBLE);

  this->set_bg_color(lv_color_hex(0), 0, 0);
}

AppIcon::AppIcon(UI_Object* parent, App* app) :Container(parent)
{
  this->set_size(72, 92);

  this->container = new Container(this);

  this->image = new Image(this->container);
  this->label = new Label(this);

  lv_obj_align(label->get_lv_obj_t(), LV_ALIGN_CENTER, 0, -10);

  lv_obj_add_flag(this->image->get_lv_obj_t(), LV_OBJ_FLAG_EVENT_BUBBLE | LV_OBJ_FLAG_EVENT_BUBBLE);
  lv_obj_add_flag(this->container->get_lv_obj_t(), LV_OBJ_FLAG_EVENT_BUBBLE | LV_OBJ_FLAG_EVENT_BUBBLE);
  lv_obj_add_flag(this->label->get_lv_obj_t(), LV_OBJ_FLAG_EVENT_BUBBLE | LV_OBJ_FLAG_EVENT_BUBBLE);


  this->set_bg_color(lv_color_hex(0), 0, 0);

  this->set_app(app);
}



AppIcon::~AppIcon()
{
  printf("AppIcon::~AppIcon\n");
}
void AppIcon::set_app(App* app)
{

  this->app = app;

  this->set_flex_layout(LV_FLEX_FLOW_COLUMN, false, false, LV_FLEX_ALIGN_SPACE_EVENLY, LV_FLEX_ALIGN_CENTER, LV_FLEX_ALIGN_CENTER);

  // this->set_bg_color(lv_color_hex(THEME_COLOR_RED), 100, 0);

  this->container->set_size(56, 56);
  this->container->set_bg_color(app->get_bg_color(), 255, LV_PART_MAIN | LV_STATE_DEFAULT);

  // this->image = new Image(this->container);
  this->image->set_size(32, 32);
  this->image->set_img(app->get_icon());


  this->label->set_text(app->get_name()->c_str(), THEME_COLOR_WHITE_PURE);

  this->set_event_cb([](lv_event_t* e) {

    printf("AppIcon::set_event_cb\n");

    lv_event_code_t  code = lv_event_get_code(e);

    if (code == LV_EVENT_CLICKED) {

      AppIcon* app_icon = (AppIcon*) e->user_data;
      app_icon->app->run((void*) apps_page);


    }

    }, LV_EVENT_CLICKED, this);



}




AppsPage::AppsPage(/* args */)
{
  app_manger.add_main_page(main_page);



  app_mount();



  this->viewpoint = new Container(this);
  this->viewpoint->set_size(220, 260);
  this->viewpoint->set_bg_color(lv_color_hex(0), 0, 0);
  this->viewpoint->add_flag(LV_OBJ_FLAG_SCROLLABLE);


  this->set_scroll_mode();
  this->load_apps_list();
  // // this->load_apps_list();
  this->render_apps_list();


  // sys.delay(1000);

  // app_manger.load_app("setting");

}

AppsPage::~AppsPage()
{
}


void AppsPage::set_scroll_mode()
{


  lv_obj_set_scrollbar_mode(this->viewpoint->get_lv_obj_t(), LV_SCROLLBAR_MODE_ON);
  lv_obj_set_scroll_dir(this->viewpoint->get_lv_obj_t(), LV_DIR_VER);



}


void AppsPage::load_apps_list()
{
  std::list<App*> apps_list = app_manger.get_app_list();


  for (auto it = this->appicons_array.begin(); it != this->appicons_array.end(); ++it)
  {
    delete* it;
  }

  this->appicons_array.clear();

  for (auto it = apps_list.begin(); it != apps_list.end(); ++it)
  {

    AppIcon* app_icon = new AppIcon(this->viewpoint);
    app_icon->set_app(*it);

    this->appicons_array.push_back(app_icon);
  }

}

void AppsPage::render_apps_list()
{
  size_t len = this->appicons_array.size();
  if (len > 1) {
    AppIcon* app_icon0 = this->appicons_array[0];
    AppIcon* app_icon1 = this->appicons_array[1];

    lv_obj_align(app_icon0->get_lv_obj_t(), LV_ALIGN_TOP_LEFT, 10, 0);
    lv_obj_align(app_icon1->get_lv_obj_t(), LV_ALIGN_TOP_RIGHT, -10, 0);
  }
  else {
    AppIcon* app_icon0 = this->appicons_array[0];
    lv_obj_align(app_icon0->get_lv_obj_t(), LV_ALIGN_TOP_LEFT, 10, 0);
  }

  for (size_t i = 2; i < len; i++)
  {
    AppIcon* app_icon = this->appicons_array[i];
    AppIcon* app_icon_front = this->appicons_array[i - 2];
    lv_obj_align_to(app_icon->get_lv_obj_t(), app_icon_front->get_lv_obj_t(), LV_ALIGN_OUT_BOTTOM_MID, 0, 20);

  }


}